﻿using PokemonClientToos.Entity;
using PokemonClientToos.TempModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace PokemonClientToos.Frm
{
    public partial class frm_actionSetting : Form
    {
        public frm_actionSetting()
        {
            InitializeComponent();
        }

        private void frm_actionSetting_Load(object sender, EventArgs e)
        {
            if (AllCache.instance.actionList != null)
            {
                dgv_actionPar.Rows.Clear();
                dgv_actionPar.DataSource = AllCache.instance.actionList;
            }
        }

        private void btn_save_Click(object sender, EventArgs e)
        {
            string actionId = txt_actionID.Text.Trim();
            string parameters = txt_parameters.Text.Trim();
            bool isLoopPar = cb_isLoopPar.Checked;
            string loopParNum = txt_loopParNum.Text.Trim();
            string loopParameters = txt_loopParameters.Text.Trim();
            if (string.IsNullOrWhiteSpace(actionId) || (isLoopPar && string.IsNullOrWhiteSpace(loopParNum) && string.IsNullOrWhiteSpace(loopParameters)))
            {
                MessageBox.Show("请输入完整的信息");
                return;
            }
            if (btn_save.Text.Trim().Equals("添加"))
            {
                TempAction action = new TempAction();
                action.ID = (AllCache.instance.actionList == null || AllCache.instance.actionList.Count == 0) ? 1 : AllCache.instance.actionList.Max(o => o.ID) + 1;
                action.ActionID = Convert.ToInt32(actionId);
                action.Parameters = parameters;
                action.IsLoopPar = isLoopPar;
                action.LoopParNum = Convert.ToInt32(loopParNum);
                action.LoopParameters = loopParameters;
                AllCache.instance.actionList.Add(action);

                AllCache.instance.Write();
            }
            else
            {
                int id = Convert.ToInt32(lb_ID.Text.Trim());
                if (AllCache.instance.actionList.Exists(o => o.ID == id))
                {
                    TempAction model = AllCache.instance.actionList.Find(o => o.ID == id);
                    model.ActionID = Convert.ToInt32(actionId);
                    model.Parameters = parameters;
                    model.IsLoopPar = isLoopPar;
                    model.LoopParNum = Convert.ToInt32(loopParNum);
                    model.LoopParameters = loopParameters;
                    AllCache.instance.Write();
                }
            }
            dgv_actionPar.DataSource = new List<TempAction>();
            dgv_actionPar.DataSource = AllCache.instance.actionList;
        }
        private void dgv_actionPar_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex < 0 || e.ColumnIndex < 0) return;
            int id = Convert.ToInt32(dgv_actionPar.Rows[e.RowIndex].Cells[1].Value);
            if (e.ColumnIndex == 0)
            {
                if (AllCache.instance.actionList.Exists(o => o.ID == id))
                {
                    TempAction tempAction = AllCache.instance.actionList.Find(o => o.ID == id);
                    AllCache.instance.actionList.Remove(tempAction);

                    AllCache.instance.Write();

                    dgv_actionPar.DataSource = new List<TempAction>();
                    dgv_actionPar.DataSource = AllCache.instance.actionList;
                }
            }
            else
            {
                if (AllCache.instance.actionList.Exists(o => o.ID == id))
                {
                    btn_save.Enabled = false;
                    TempAction tempAction = AllCache.instance.actionList.Find(o => o.ID == id);
                    lb_ID.Text = tempAction.ID.ToString();
                    txt_actionID.Text = tempAction.ActionID.ToString();
                    txt_parameters.Text = tempAction.Parameters.ToString();
                    cb_isLoopPar.Checked = tempAction.IsLoopPar;
                    txt_loopParNum.Text = tempAction.LoopParNum.ToString();
                    txt_loopParameters.Text = tempAction.LoopParameters;
                    btn_save.Text = "修改";
                    btn_save.Enabled = true;
                    btn_exit.Visible = true;
                }
            }
        }

        private void btn_exit_Click(object sender, EventArgs e)
        {
            btn_save.Enabled = false;
            lb_ID.Text = "0";
            txt_actionID.Text = "";
            txt_parameters.Text = "";
            cb_isLoopPar.Checked = false;
            txt_loopParNum.Text = "0";
            txt_loopParameters.Text = "";
            btn_exit.Visible = false;
            btn_save.Text = "添加";
            btn_save.Enabled = true;
        }
    }
}
